;$(function () {

    //判断是否为四位以内的正整数
    window.isInt4 = function (field, rules, i, options) {
        var thisValue = field[0].value;
        var reg = /^[1-9]\d{0,3}$/;
        if (!reg.test(thisValue)) {
            var message = "请输入四位以内的正整数！"
            return message;
        }
    }
    $("#submitBtn").click(function () {
        $('a.hide-next-btn-show').each(function (i, val) {
            $(this).click();
        });
        if ($('#form').validationEngine('validate')) {
            var array = $("#form").serializeArray();
            var paramArray = new Array();

            for (var i = 0; i < parseInt(array.length / 3); i++) {
                var _queryParams = {};
                for (var k = i * 3; k < i * 3 + 3; k++) {
                    _queryParams[array[k].name] = array[k].value.trim();
                }
                paramArray.push(_queryParams);
            }
            return $.ajax({
                type: "POST",
                url: basePath + "/studentQuestion/submitQuestions.json",
                dataType: 'json',
                data: JSON.stringify(paramArray),
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    BootstrapDialog.show({
                        title: "提示框",
                        message: "提交成功",
                        buttons: [{
                            label: "确认",
                            action: function (dialog) {
                                window.close();
                            }
                        }]
                    });
                }
            });
        }
    });

    $("#addBtn").click(function () {
        var tr =
            '<tr>' +
            '<td class="col-xs-4">' +
            '<select name="bookClass" class="form-control validate[required]">' +
            '<option value="" selected="selected">请选择</option>' +
            '</select>' +
            '</td>' +
            '<td class="col-xs-2">' +
            '<select name="bookSubject" class="form-control validate[required]">' +
            '<option value="" selected="selected">请选择</option>' +
            '</select>' +
            '</td>' +
            '<td class="col-xs-2">' +
            '<input type="text" class="form-control validate[required,funcCallRequired[isInt4]]" name="quesCode" value="" placeholder="请填写数字"/>' +
            '</td>' +
            '<td class="col-xs-2">' +
            '<button type="button" class="btn btn-primary remove">' +
            '<i class="fa fa-trash">&nbsp;删除&nbsp;</i>' +
            '</button>' +
            '</td>' +
            '</tr>';
        var url = basePath + "/dictionay/getByModel.json";
        $.ajax({
            url: url,
            type: "get",
            data: {"model": "book_class"},
            dataType: "json",
            success: function (data) {
                $tr = $(tr);
                for (var i = 0; i < data.length; i++) {
                    var option = '<option value="' + data[i].dicCode + '">' + data[i].dicValue + '</option>';
                    $tr.find("select[name='bookClass']").append(option);
                }
                $(".form-table").append('<tr>' + $tr.html() + '</tr>');
            }
        });
    });

    $("table").on("change", "select[name='bookClass']", function () {
        var bookClass = $(this).val();
        var $bookSubject = $(this).parent().parent().find("select[name='bookSubject']");
        $bookSubject.find("option").remove();
        $bookSubject.append('<option value="" selected="selected">请选择</option>');
        if (bookClass != '') {
            var url = basePath + "/dictionay/getChildrenByModelAndCode.json";
            $.ajax({
                url: url,
                type: "get",
                data: {"code": bookClass, "model": "book_class"},
                dataType: "json",
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var option = '<option value="' + data[i].dicCode + '">' + data[i].dicValue + '</option>';
                        $bookSubject.append(option);
                    }
                }
            });
        }
    });

    $("table").on("click", ".remove", function () {
        $(this).parent().parent().remove();
    });

});